package com.cyl.h5.controller;

import com.cyl.h5.config.SecurityUtil;
import com.cyl.manager.act.domain.convert.FhVipExchangeCodeConvert;
import com.cyl.manager.act.domain.entity.FhVipExchangeCode;
import com.cyl.manager.act.domain.query.FhVipExchangeCodeQuery;
import com.cyl.manager.act.domain.vo.FhVipExchangeCodeVO;
import com.cyl.manager.act.service.FhVipExchangeCodeService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * VIP兑换码表Controller
 *
 * @author zcc
 * @date 2025-10-09
 */
@Api(description ="VIP兑换码表接口列表")
@RestController
@RequestMapping("/app/fhVipExchangeCode")
public class H5FhVipExchangeCodeController extends BaseController {
    @Autowired
    private FhVipExchangeCodeService service;


    @ApiOperation("获取VIP兑换码表详细信息")
    @PreAuthorize("@ss.hasPermi('act:fhVipExchangeCode:query')")
    @GetMapping(value = "/{id}")
    public ResponseEntity<FhVipExchangeCode> getInfo(@PathVariable("id") Long id) {
        return ResponseEntity.ok(service.selectById(id));
    }

    @ApiOperation("修改VIP兑换码表")
    @Log(title = "VIP兑换码表", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResponseEntity<Integer> edit(@RequestBody FhVipExchangeCode fhVipExchangeCode) {
        return ResponseEntity.ok(service.update(fhVipExchangeCode));
    }

    /**
     * 兑换VIP码
     */
    @ApiOperation("兑换VIP码")
    @PostMapping("/redeem")
    public AjaxResult redeemVipCode(@RequestParam String code) {
        try {
            // 获取当前登录用户ID
            Long userId = SecurityUtil.getLocalUserInfo().getId();
            // 调用兑换服务
            service.redeemVipCode(code, userId);

            return AjaxResult.success("兑换成功");
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

}
